package clientGUI;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;


/**
 * @author Roee
 *
 */
public class MainGUI {

	public static JFrame frame;
	public static JPanel panelButtons;
	public static JPanel panelChanging;
	public static ChatClient OBLClient;
	public static boolean done;
	public static boolean testmode;
	
	public static String ordered;
	public MainGUI(String ip,int port) throws IOException {
		
		OBLClient=new ChatClient(ip,port);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		done=true;
		testmode=false;
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent arg0) {
				try {
					quit();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		frame.setTitle("Ort Braude Library");
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("images/Logo.jpg"));
		frame.setResizable(false);
		frame.setBounds(100, 100, 871, 581);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setBounds(0, 0, 644, 85);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblOrtBraudeLibrary = new JLabel("Ort Braude Library");
		lblOrtBraudeLibrary.setFont(new Font("Tahoma", Font.BOLD, 40));
		lblOrtBraudeLibrary.setBounds(150, 13, 383, 59);
		panel.add(lblOrtBraudeLibrary);
		JLabel lblNewLabel3 = new JLabel("");
		lblNewLabel3.setIcon(new ImageIcon("images/test36.jpg"));
		lblNewLabel3.setBounds(4, 5, 638, 76);
		panel.add(lblNewLabel3);
		UserGUI x= UserGUI.getInstance();
		LoginPanel y=new LoginPanel();
		panelButtons = new JPanel();
		panelButtons.setLayout(null);
		panelButtons.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		panelButtons.setBackground(SystemColor.control);
		panelButtons.setBounds(643, 0, 222, 547);
		x.setBounds(0, 0, 219, 547);
		panelButtons.add(x);
		frame.getContentPane().add(panelButtons);
		
		panelChanging = new JPanel();
		panelChanging.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panelChanging.setBackground(SystemColor.menu);
		panelChanging.setBounds(0, 85, 644, 462);
		frame.getContentPane().add(panelChanging);
		panelChanging.setLayout(null);
		middlePanelChange(y);
		JLabel lblChangingPanel = new JLabel();
		lblChangingPanel.setFont(new Font("Tahoma", Font.ITALIC, 40));
		lblChangingPanel.setBounds(163, 172, 349, 119);
		panelChanging.add(lblChangingPanel);
	}

	public void quit() throws IOException {
		String temp[]={"quit"};
		MainGUI.OBLClient.handleMessageFromClient(temp);
		MainGUI.OBLClient.closeConnection();
		File f=new File("temp.pdf");
		f.delete();
		System.exit(1);
		
	}

	/**
	 * @param p
	 * Replaces the current panel in panelChanging with panel p
	 */
	static void middlePanelChange(JPanel p) {
		p.setBounds(0, 0, 644, 462);
		panelChanging.removeAll();
		panelChanging.add(p);
		panelChanging.repaint();
	}
	static void buttonsPanelChange(String x,String first,String last) {
		JPanel p=new JPanel();
		switch(x)
		{
		case "0":
			p=UserGUI.getInstance();
			break;
		case "1":
			 p=SubscriberGUI.getInstance();
			((SubscriberGUI)p).btnViewMyReaderCard.doClick();
			break;
		case "2":
			p=LibrarianGUI.getInstance();
			((LibrarianGUI)p).btnLibraryMessages.doClick();
			break;
		case "3":
			p=ManagerGUI.getInstance();
			((ManagerGUI)p).btnLibraryMessages.doClick();
			break;
		}
		p.setBounds(0, 0, 222, 547);
		panelButtons.removeAll();
		panelButtons.add(p);
		panelButtons.repaint();
		if(!testmode)
		{
			if(x.contentEquals("0"))
				JOptionPane.showMessageDialog(new JFrame(),	"Logged out successfully!");
			else 
				JOptionPane.showMessageDialog(new JFrame(),	"Welcome! " + first + " " + last );
		}
	}

	/**
	 * Checking if the input contains only digits
	 * @param s - input
	 * @return true/false
	 */
	public static boolean isNumber(String s) 
	{
		char[] isNum=s.toCharArray();
		if(isNum[0]!='-' && !Character.isDigit(isNum[0]))
		{
			return false;
		}
		for(int i=1;i<isNum.length;i++)
		{
			if(!Character.isDigit(isNum[i]))
			{
				return false;
			}
		}
		return true;
	}
	
	/**
	 * uses regex to check if email pattern is legal.
	 * @param mail
	 * @return true if email is ok, or false otherwise.
	 */
	public static boolean validateEmail(final String mail) { 
		Matcher matcher;
		Pattern pattern1;
		String EMAIL_PATTERN = 
				"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		pattern1 = Pattern.compile(EMAIL_PATTERN);
		matcher = pattern1.matcher(mail);
		return matcher.matches();
 
	}
	
	/**
	 * Checking if the input contians only letters
	 * @param s
	 * @return true/false
	 */
	public static boolean isName(String s) 
	{
		char[] isName=s.toCharArray();
		for(int i=0;i<isName.length;i++)
		{
			if(!Character.isLetter(isName[i]))
			{
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Checking if the input dosen't contain anything
	 * @param msg
	 * @return true/false
	 */
	public static boolean isMsgEmpty(String[] msg)
	{
		for(int i=0;i<msg.length;i++)
		{
			if(msg[i].isEmpty())
				return true;
		}
		return false;
	}
}
